package com.itheima.service.com.itheima.service.system.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.system.ModuleDao;
import com.itheima.dao.system.UserDao;
import com.itheima.domain.system.Module;
import com.itheima.domain.system.User;
import com.itheima.factory.MapperFactory;
import com.itheima.service.com.itheima.service.system.UserService;
import com.itheima.utils.MD5Util;
import com.itheima.utils.TransactionUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.UUID;

public class UserServiceImpl implements UserService {

    @Override
    public void save(User user) {

        SqlSession sqlSession = null;
        try {
            //获取SQLSession对象
            sqlSession = MapperFactory.getSqlSession();
            //获取Dao
            UserDao userDao = MapperFactory.getMapper(sqlSession, UserDao.class);
            //创建随机的UUID作为ID
            String id = UUID.randomUUID().toString();

            user.setId(id);
            user.setPassword(MD5Util.md5(user.getPassword()));
            //调用Dao中的方法保存数据
            userDao.save(user);
            //提交事务
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            //回滚事务
            TransactionUtil.rollback(sqlSession);
            //抛出异常
            throw new RuntimeException(e);
        } finally {
            try {
                //关闭SQLSession对象
                sqlSession.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void delete(User user) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            UserDao mapper = MapperFactory.getMapper(sqlSession, UserDao.class);
            int delete = mapper.delete(user);
            TransactionUtil.commit(sqlSession);
            sqlSession.close();
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
        } finally {
            try {
                sqlSession.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    @Override
    public void update(User user) {

        //获取SQLSession对象
        SqlSession sqlSession = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            //通过
            UserDao userDao = MapperFactory.getMapper(sqlSession, UserDao.class);

            userDao.update(user);

            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
        } finally {
            try {
                sqlSession.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public User findById(String id) {
        //获取SQLSession对象
        SqlSession sqlSession = null;
        User user = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            //通过
            UserDao userDao = MapperFactory.getMapper(sqlSession, UserDao.class);

            user = userDao.findById(id);

            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
        } finally {
            try {
                sqlSession.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return user;
    }

    @Override
    public List<User> findAll() {
        List<User> companies = null;
        SqlSession sqlSession = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            UserDao userDao = MapperFactory.getMapper(sqlSession, UserDao.class);
            companies = userDao.findAll();
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
        } finally {
            sqlSession.close();
        }
        return companies;
    }

    @Override
    public PageInfo findAll(int page, int size) {

        SqlSession sqlSession = null;
        try {
            //1.获取SqlSession
            sqlSession = MapperFactory.getSqlSession();
            //2.获取Dao
            UserDao userDao = MapperFactory.getMapper(sqlSession, UserDao.class);
            //3.调用Dao层操作
            PageHelper.startPage(page, size);
            List<User> all = userDao.findAll();
            PageInfo pageInfo = new PageInfo(all);
            return pageInfo;
        } catch (Exception e) {
            throw new RuntimeException(e);
            //记录日志
        } finally {
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void updateRole(String userId, String[] roleIds) {
        SqlSession sqlSession = null;
        try {
            //1.获取SqlSession
            sqlSession = MapperFactory.getSqlSession();
            //2.获取Dao
            UserDao userDao = MapperFactory.getMapper(sqlSession, UserDao.class);
            //3.调用Dao层操作
            userDao.deleteRole(userId);
            for (String roleId : roleIds) {

                userDao.updateRole(userId, roleId);
            }
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            throw new RuntimeException(e);
            //记录日志
        } finally {
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public User login(String email, String password) {
        SqlSession sqlSession = null;
        try {
            //1.获取SqlSession
            sqlSession = MapperFactory.getSqlSession();
            //2.获取Dao
            UserDao userDao = MapperFactory.getMapper(sqlSession, UserDao.class);
            //3.调用Dao层操作
             password = MD5Util.md5(password);
             return userDao.findByEmailAndPwd(email,password);

        } catch (Exception e) {
            throw new RuntimeException(e);
            //记录日志
        } finally {
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public List<Module> findModuleById(String id) {

        SqlSession sqlSession = null;
        try {
            //1.获取SqlSession
            sqlSession = MapperFactory.getSqlSession();
            //2.获取Dao
            ModuleDao moduleDao = MapperFactory.getMapper(sqlSession, ModuleDao.class);
            //3.调用Dao层操作
            return moduleDao.findModuleByUserId(id);
        } catch (Exception e) {
            throw new RuntimeException(e);
            //记录日志
        } finally {
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}